import {take,call,put,select} from 'redux-saga/effects'
import {get, post, put_http} from '../axios'
import {actionsTypes as IndexActionTypes} from '../reducers'
import {actionsTypes as ProjectActionTypes} from '../reducers/project'
import history from '../history';

export function* get_list (type, page, search) {
    yield put({type: IndexActionTypes.FETCH_START});
    try {
        return yield call(get, `/mem/project-my-${type}?page=${page}${search}`);
    } catch (err) {
        yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '网络请求错误', msgType: 'fail'});
    } finally {
        yield put({type: IndexActionTypes.FETCH_END})
    }
}

export function* get_detail (id, type) {
    yield put({type: IndexActionTypes.FETCH_START});
    try {
        return yield call(get, `/mem/project-my-${type}/${id}`);
    } catch (err) {
        yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '网络请求错误', msgType: 'fail'});
    } finally {
        yield put({type: IndexActionTypes.FETCH_END})
    } 
}


export function* getProjectListFlow () {
    while (true){
        let req = yield take(ProjectActionTypes.GET_PRO_LIST);
        let res = yield call(get_list, req._type, req.page, req.search);
        if(res){
            if (res.current_page) {
                yield put({type: ProjectActionTypes.RESPONSE_PRO_LIST, data: res, _type: req._type})
            } else {
                yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: res&&res.msg, msgType: 'fail'});
            }
        }
    }
}

export function* getProjectDetailFlow () {
    while (true) {
        let req = yield take(ProjectActionTypes.GET_DETAIL)
        let res = yield call(get_detail, req.id, req._type)
        if (res&&res.success) {
            yield put({type: ProjectActionTypes.RESPONSE_DETAIL, data: res.data,  _type: req._type})
        } else {
            if (res&&res.code==404) {
                history.goBack()
            } else {
                yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: res&&res.msg, msgType: 'fail'});
            }
        }
    }
}

export function* save_project (data, mold, id) {
    yield put({type: IndexActionTypes.FETCH_START});
    try {
        if (id) {
            return yield call(put_http, `/mem/project-my-publish/${id}?status=${mold}`, data);
        } else {
            return yield call(post, `/mem/project-my-publish?status=${mold}`, data);
        }
    } catch (err) {
        yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '网络请求错误', msgType: 'fail'});
    } finally {
        yield put({type: IndexActionTypes.FETCH_END})
    } 
}

export function* saveProjectFlow () {
    while (true){
        let req = yield take(ProjectActionTypes.SAVE_PROJECT);
        let id = req.id ? req.id : undefined
        console.log(req.data)
        let res = yield call(save_project, req.data, req.mold, id);
        if (res&&res.success) {
            yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '操作成功', msgType: 'success'});
            console.log(req)
            if (req.mold=='publish' && req.data.assign_type=='custom') {
                yield call( ()=>setTimeout( function () {
                    history.replace({
                        pathname: '/assign',
                        search: `?project_id=${res.data.id}`, 
                    })
                }, 1000))
            } else {
                yield call( ()=>setTimeout( function () {
                    history.goBack()
                }, 1000))
            }
        } else {
            yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: res&&res.msg, msgType: 'fail'});
        }
    }
    
}

export function* get_user_parent_project () {
    yield put({type: IndexActionTypes.FETCH_START});
    try {
        return yield call(get, `/mem/parent-project-list`);
    } catch (err) {
        yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '网络请求错误', msgType: 'fail'});
    } finally {
        yield put({type: IndexActionTypes.FETCH_END})
    }
}

export function* getUserParentProjectFlow () {
    while (true){
        let req = yield take(ProjectActionTypes.GET_USER_PAARENT_PROJECT_LIST);
        let res = yield call(get_user_parent_project);
        if (res) {
            yield put({type: ProjectActionTypes.RESPONSE_USER_PAARENT_PROJECT_LIST, data: res})
        } else {
            yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: res&&res.msg, msgType: 'fail'});
        }
    }
}

export function* accept_project (id) {
    yield put({type: IndexActionTypes.FETCH_START});
    try {
        return yield call(get, `/mem/project-my-grab/${id}/accept`);
    } catch (err) {
        yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '网络请求错误', msgType: 'fail'});
    } finally {
        yield put({type: IndexActionTypes.FETCH_END})
    }
}

export function* acceptProjectFlow () {
    while (true){
        let req = yield take(ProjectActionTypes.ACCEPT_PROJECT);
        let res = yield call(accept_project, req.id);
       
        if (res&&res.success) {
            yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '操作成功', msgType: 'success'});
            yield call( ()=>setTimeout( function () {
                history.replace(`/my-project/execute`)
            }, 1000))
        } else {
            yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: res&&res.msg, msgType: 'fail'});
        }
    }
}

export function* get_children (project_id) {
    yield put({type: IndexActionTypes.FETCH_START});
    try {
        return yield call(get, `/mem/project-child/${project_id}`);
    } catch (err) {
        yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '网络请求错误', msgType: 'fail'});
    } finally {
        yield put({type: IndexActionTypes.FETCH_END})
    }
}

export function* getChildrenFlow () {
    while (true){
        let req = yield take(ProjectActionTypes.GET_CHILDREN);
        let res = yield call(get_children, req.project_id);
        if (res&&res.success) {
            yield put({type: ProjectActionTypes.RESPONSE_CHILDREN, data: res.data})
        } else {
            yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: res&&res.msg, msgType: 'fail'});
        }
    }
}

export function* delete_project (project_id) {
    yield put({type: IndexActionTypes.FETCH_START});
    try {
        return yield call(post, `/mem/project-my-publish/${project_id}/delete`);
    } catch (err) {
        yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '网络请求错误', msgType: 'fail'});
    } finally {
        yield put({type: IndexActionTypes.FETCH_END})
    }
}

export function* deleteProjectFlow () {
    while (true){
        let req = yield take(ProjectActionTypes.DELETE_PROJECT);
        let res = yield call(delete_project, req.project_id);
        if (res&&res.success) {
            yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '删除成功', msgType: 'success'});
            yield call( ()=>setTimeout( function () {
                history.go(-1)
            }, 1000))
        } else {
            yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: res&&res.msg, msgType: 'fail'});
        }
    }
}

export function* cancel_project (project_id, role) {
    yield put({type: IndexActionTypes.FETCH_START});
    try {
        return yield call(post, `/mem/project-my-${role}/${project_id}/cancel`);
    } catch (err) {
        yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '网络请求错误', msgType: 'fail'});
    } finally {
        yield put({type: IndexActionTypes.FETCH_END})
    }
}

export function* cancelProjectFlow () {
    while (true){
        let req = yield take(ProjectActionTypes.CANCEL_PROJECT);
        let res = yield call(cancel_project, req.project_id, req.role);
        if (res&&res.success) {
            yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '取消成功', msgType: 'success'});
            yield call( ()=>setTimeout( function () {
                history.go(-1)
            }, 1000))
        } else {
            yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: res&&res.msg, msgType: 'fail'});
        }
    }
}

export function* publish_project (project_id) {
    yield put({type: IndexActionTypes.FETCH_START});
    try {
        return yield call(get, `/mem/grab-project-publish/${project_id}`);
    } catch (err) {
        yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '网络请求错误', msgType: 'fail'});
    } finally {
        yield put({type: IndexActionTypes.FETCH_END})
    }
}

export function* publishProjectFlow () {
    while (true){
        let req = yield take(ProjectActionTypes.PUBLISH_PROJECT);
        let res = yield call(publish_project, req.project_id);
        if (res&&res.success) {
            yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '发布成功', msgType: 'success'});
            yield call( ()=>setTimeout( function () {
                history.go(-1)
            }, 1000))
        } else {
            yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: res&&res.msg, msgType: 'fail'});
        }
    }
}

function* complete_project (id) {
    yield put({type: IndexActionTypes.FETCH_START});
    try {
        return yield call(post, `/mem/project-my-execute/${id}/done`);
    } catch (err) {
        yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '网络请求错误', msgType: 'fail'});
    } finally {
        yield put({type: IndexActionTypes.FETCH_END})
    }
}

export function* completeProjectFlow () {
    while (true){
        let req = yield take(ProjectActionTypes.COMPLETE_PROJECT);
        let res = yield call(complete_project, req.id);
        if (res&&res.success) {
            yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '操作成功', msgType: 'success'});
            let url = `/project-detail/comment?type=execute&id=${req.id}&project_id=${req.project_id}`
            req.hide_comment ? url = `/project-detail/introduction?type=execute&id=${req.id}&project_id=${req.project_id}`:null
            yield call( ()=>setTimeout( function () {
                history.replace(url)
            }, 1000))
        } else {
            yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: res&&res.msg, msgType: 'fail'});
        }
    }
}

function* reject_project (id) {
    yield put({type: IndexActionTypes.FETCH_START});
    try {
        return yield call(post, `/mem/project-my-grab/${id}/reject`);
    } catch (err) {
        yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '网络请求错误', msgType: 'fail'});
    } finally {
        yield put({type: IndexActionTypes.FETCH_END})
    }
}

export function* rejectProjectFlow () {
    while (true){
        let req = yield take(ProjectActionTypes.REJECT_PROJECT);
        let res = yield call(reject_project, req.id);
        if (res&&res.success) {
            yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '拒绝成功', msgType: 'success'});
            yield call( ()=>setTimeout( function () {
                history.replace(`/my-project/grab`)
            }, 1000))
        } else {
            yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: res&&res.msg, msgType: 'fail'});
        }
    }
}